package com.jjc.android.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;

import com.jjc.android.view.MaterialProgressDrawable;
import com.jjc.android.R;
import com.jjc.android.view.MaterialProgressDrawable;


public class LoadingDialog extends AlertDialog {

    private MaterialProgressDrawable materialProgressDrawable;
    private String message = null;
    private ImageView ivDialogLoading;
    private TextView tv_dialog_loading;

    public LoadingDialog(@NonNull Context context) {
        super(context);
        this.setCancelable(false);
        reset();
    }

    public LoadingDialog(@NonNull Context context, String message) {
        super(context);
        this.message = message;
        this.setCancelable(false);
    }

    public LoadingDialog(@NonNull Context context, int theme, String message) {
        super(context, theme);
        this.message = message;
        this.setCancelable(false);
    }

    public void setMessage(String messsage){
        if(tv_dialog_loading!=null){
            tv_dialog_loading.setText(messsage);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.base_dialog_loading_dt);
        if (getWindow() != null) {
            getWindow().setBackgroundDrawable(new ColorDrawable());
            getWindow().getAttributes().dimAmount = 0.2f;
        }

        ivDialogLoading = (ImageView) findViewById(R.id.iv_dialog_loading);
        tv_dialog_loading = findViewById(R.id.tv_dialog_loading);
        if(!TextUtils.isEmpty(message)){
            tv_dialog_loading.setText(message);
        }
        materialProgressDrawable = new MaterialProgressDrawable(getContext(), ivDialogLoading);
        materialProgressDrawable.setColorSchemeColors(ContextCompat.getColor(getContext(), R.color.colorPrimary));
        materialProgressDrawable.setAlpha(255);
        materialProgressDrawable.updateSizes(MaterialProgressDrawable.LARGE);
        ivDialogLoading.setImageDrawable(materialProgressDrawable);
    }

    @Override
    public void show() {
        super.show();
        if (materialProgressDrawable != null) {
            materialProgressDrawable.start();
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (materialProgressDrawable != null) {
            materialProgressDrawable.stop();
        }
    }

    public void reset() {
        if (materialProgressDrawable != null) {
            materialProgressDrawable.stop();
            materialProgressDrawable = null;
        }
    }

    public interface DialogControl {

        void hideLoadingDialog();

        LoadingDialog showLoadingDialog();

    }
}
